package com.ie.service.before;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.ConfigrarionBean;
import com.ie.common.security.utils.JwtTokenUtil;
import com.ie.common.security.utils.MD5Util;
import com.ie.common.security.utils.RedisUtil;
import com.ie.entity.BUserEntity;
import com.ie.mapper.before.BUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BUserServiceImpl extends ServiceImpl<BUserMapper, BUserEntity> implements BUserService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private ConfigrarionBean config;

    @Override
    public ResponseResult<Map<String, String>> register(BUserEntity bUserEntity) {
        bUserEntity.setBpwd(MD5Util.MD5(bUserEntity.getBpwd()));
        Long count = lambdaQuery().eq(BUserEntity::getBemail, bUserEntity.getBemail()).count();
        if (count > 0) {
            return ResponseResult.getMessageResult(null, "A001");
        } else if (save(bUserEntity)) {
            return ResponseResult.getMessageResult(null, "A002");
        } else {
            return ResponseResult.getMessageResult(null, "A003");
        }
    }

    @Override
    public ResponseResult<Map<String, String>> login(BUserEntity bUserEntity) {
        try {
            // 验证码验证
            String code = (String) redisUtil.get("code");
            if (code == null || !code.equalsIgnoreCase(bUserEntity.getCode())) {
                return ResponseResult.getMessageResult(null, "A000");
            }

            // 检查用户是否存在
            Long count = lambdaQuery().eq(BUserEntity::getBemail, bUserEntity.getBemail()).count();
            if (count == 0) {
                return ResponseResult.getMessageResult(null, "A001");
            }

            // 密码加密并验证
            bUserEntity.setBpwd(MD5Util.MD5(bUserEntity.getBpwd()));
            List<BUserEntity> list = lambdaQuery()
                    .eq(BUserEntity::getBemail, bUserEntity.getBemail())
                    .eq(BUserEntity::getBpwd, bUserEntity.getBpwd()).list();

            // 验证成功，生成token并返回
            if (list.size() > 0) {
                String token = jwtTokenUtil.createToken(bUserEntity.getBemail());
                Map<String, String> myres = new HashMap<>();
                myres.put("buserauthtoken", token);
                myres.put("bemail", bUserEntity.getBemail());
                myres.put("bid", list.get(0).getId() + "");

                // 保存用户登录状态到Redis
                redisUtil.set("login_" + bUserEntity.getBemail(), bUserEntity.getBemail(), config.getRedisExpiration());

                return ResponseResult.getSuccessResult(myres);
            } else {
                return ResponseResult.getMessageResult(null, "A002");
            }
        } catch (Exception e) {
            log.error("登录过程中出现异常", e);
            return ResponseResult.getMessageResult(null, "A999");
        }
    }



    @Override
    public ResponseResult<String> changePassword(BUserEntity bUserEntity) {
        // 参数校验
        if (bUserEntity == null || bUserEntity.getBemail() == null ||
                bUserEntity.getOldPwd() == null || bUserEntity.getNewPwd() == null) {
            return ResponseResult.getMessageResult(null, "A000"); // 参数错误
        }

        BUserEntity user = lambdaQuery()
                .eq(BUserEntity::getBemail, bUserEntity.getBemail())
                .one();

        if (user == null) {
            return ResponseResult.getMessageResult(null, "A001"); // 用户不存在
        }

        if (!user.getBpwd().equals(MD5Util.MD5(bUserEntity.getOldPwd()))) {
            return ResponseResult.getMessageResult(null, "A002"); // 旧密码错误
        }

        if (user.getBpwd().equals(MD5Util.MD5(bUserEntity.getNewPwd()))) {
            return ResponseResult.getMessageResult(null, "A003"); // 新密码不能与旧密码相同
        }
        user.setBpwd(MD5Util.MD5(bUserEntity.getNewPwd()));
        boolean success = updateById(user);

        if (success) {
            return ResponseResult.getMessageResult(null, "A004"); // 修改成功
        } else {
            return ResponseResult.getMessageResult(null, "A005"); // 修改失败
        }
    }
    }
